/**	customNameVillages.js
 *	names player's villages on overviews (screen=overview_villages)
 *	author: Pulp Fiction (w71)
 *	game compatability:	version	20017 8.20
 */ 

/*==== GUI ====*/
if(!tws) var tws={};

/*==== renamer ====*/
tws.renameVillages = function(mode)
{
	try{
	$('.quickedit-vn').each(function(key,village){ //each village
		var villageId = $(village).attr('data-id');
		var $label = $(village).find('.quickedit-label');
		var originalFullName = $label.text();
		var originalName = $label.attr('data-text');

		var continent = originalFullName.match(/[0-9]{1,2}/gi).pop();
		var coordinates = originalFullName.match(/[0-9]{1,}\|[0-9]{1,}/gi).pop();
		var coordX = coordinates.match(/[0-9]{1,}/);
		var coordY = String(coordinates.match(/\|[0-9]{1,}/)).substring(1);			
		
		var name = '';
		
		var sector = String(coordX).substring(1,2) + String(coordY).substring(1,2)
		var field = String(coordX).substring(2) + String(coordY).substring(2)
		name += continent + '.' + sector + '.' + field;
    name += ' *cake*';

		if(mode == 'overwrite')
			name = name;
		else if(mode == 'append')
			name = originalName + name;
		else if(mode == 'prepend')
			name = name + originalName;
		
		if(name.length <= 32){
			$(village).find('.rename-icon').click();
			$(village).find('input[type=text]').val(name);
			$(village).find('input[type=button]').click();
		}
		else
			UI.InfoMessage('Name too lengthy (max 32 characters).<br/>New name not applied to all villages.',5000,'error');
	});
	}catch(e){alert(e)}
}

/*==== main ====*/
if(game_data.screen == 'overview_villages' || canNameVillages){
	tws.renameVillages('overwrite');
	var canNameVillages = true; //hack for bug where renaming villages changes game_data.screen

}
else{
	UI.InfoMessage('Use this on one of the village overviews.',5000,'error');
}